package cn.xiongxf.org.controller;

import cn.xiongxf.basic.util.AjaxResult;
import cn.xiongxf.basic.util.PageList;
import cn.xiongxf.org.domain.Employee;
import cn.xiongxf.org.query.EmployeeQuery;
import cn.xiongxf.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工API",description = "员工的CRUD操作") // 描述Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有") // 描述方法
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 接口：查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public Employee findById(@PathVariable Long id){
        return employeeService.findById(id);
    }

    /**
     * 接口：分页查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPageList(employeeQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult del(@PathVariable Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try{
            employeeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：新增/修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增/修改")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "根据店铺id查询所有店铺人员")
    public List<Employee> findByShopId(@PathVariable("id") Long id){
        return employeeService.findByShopId(id);
    }

}
